package zhu.common.data;

import java.util.List;

public class Pager {
	private int totalRows;//记录总数
	private int pageSize = 5;//设置一页显示的记录数
	private int currentPage;//当前页码
	private int totalPages;//总页数
	private int startRow;//当前页码开始记录数
	private List elements;//记录列表
	public Pager() {
	}
	//构造pager对象
	public Pager(int _totalRows, int pageSize) {
		this.pageSize=pageSize;
		totalRows = _totalRows;
		totalPages=totalRows/pageSize;
		int mod=totalRows%pageSize;
		if(mod>0){
			totalPages++;//最后一页不足一页则总页数加1
		}
		currentPage = 1;//初始设置当前页为第一页
		startRow = 0;//初始读取设置为0
	}
	
	//点击首页按钮，执行这个方法
	public void first() {
		currentPage = 1;
		startRow = 0;
	}
	//点击上一页按钮，执行这个方法
	public void previous() {
		if (currentPage == 1) {
			return;
		}
		currentPage--;
		startRow = (currentPage - 1) * pageSize;
	}
	//点击下一页按钮，执行这个方法
	public void next() {
		if (currentPage < totalPages) {
			currentPage++;
		}
		startRow = (currentPage - 1) * pageSize;
	}
	//点击尾页按钮，执行这个方法
	public void last() {
		currentPage = totalPages;
		startRow = (currentPage - 1) * pageSize;
	}
	
	//做页面跳转时，执行这个方法
	public void refresh(int _currentPage) {
		currentPage = _currentPage;
		if (currentPage > totalPages) {
			last();//_currentPage超过了总页数，则跳转到最后一页
		}
		if(currentPage<1){
			first();//_currentPage不足1，则跳转到首页
		}
		startRow = (currentPage - 1) * pageSize;//重新设置起始读取位置
	}
	
	public int getStartRow() {
		return startRow;
	}
	public int getTotalPages() {
		return totalPages;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}
	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getTotalRows() {
		return totalRows;
	}
	public List getList() {
        return elements;
    }
	public void setList(List elements) {
        this.elements=elements;
    }
}